using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using BriansClimb;
using BriansClimb.GameObjects;
using BriansClimb.Climber;
using BriansClimb.DrawableGameObjects.Buttons;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;

namespace BriansClimb.DrawableGameObjects.GameScenes
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PlayerCustomizeScene : GameScene
    {
        protected Game game;
        protected SpriteBatch spriteBatch;        
        protected Texture2D background;
        protected Texture2D heightWeightBack;
        protected Texture2D strFlexBack;        
        protected SpriteFont normalFont;                
        protected Vector2 barPosition;       
        protected SliderBar weightBar;
        protected SliderBar strengthBar;
        protected SliderBar flexBar;                
        public LeftClickButton nextButton;
        public LeftClickButton backButton;
        protected MouseState mouseState;
        protected FileManager fileManager;
        protected FileStatManager fileStatManager;
        protected AudioManager audioManager;

        //The attribute arrays. If you want to add/change/remove values do it here.
        protected int[] weightArray = { 140, 145, 150, 155, 160, 165, 170, 175, 180, 185 };        
        protected int[] strengthArray = { 1, 2, 3, 4, 5 };
        protected int[] flexArray = { 1, 2, 3, 4, 5 };
        protected int arrayIndex = 0;
        protected string wValue = " ";
        protected string strValue = " ";
        protected string flxValue = " ";
        protected int weight, oldWeight;
        protected int strength, oldStrength;
        protected int flex, oldFlex;
        protected bool clicked;
        protected bool pressed;
        bool first = true;
                       
        /// <summary>
        /// The constructor for PlayerCustomizeScene.
        /// Assigns the variables passed from Game1, loads the textures and creates the SliderBars.
        /// </summary>
        /// <param name="game"></param>
        /// <param name="fileManager"></param>
        /// <param name="background"></param>
        /// <param name="normalFont"></param>
        /// <param name="selectedFont"></param>
        public PlayerCustomizeScene(Game game, FileManager fileManager, FileStatManager fileStatManager, Texture2D background,
                                  SpriteFont normalFont, SpriteFont selectedFont)
            : base(game)
        {
            spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));            

            this.fileManager = fileManager;
            this.background = game.Content.Load<Texture2D>(".\\Textures\\BackgroundPlayerCustomize");
            this.normalFont = normalFont;
            this.fileStatManager = fileStatManager;
            this.game = game;
            audioManager = (AudioManager)game.Services.GetService(typeof(AudioManager));
            
            mouseState = Mouse.GetState();

            nextButton = new LeftClickButton(game, "next", 1075, 50);
            Components.Add(nextButton);

            backButton = new LeftClickButton(game, "back", 50, 50);
            Components.Add(backButton);
            
            //Adds the three sliderBars to scene
            barPosition.X = 560;
            barPosition.Y = 394;
            weightBar = new SliderBar(game, 500, weightArray.Length, barPosition);                       
            
            barPosition.Y = 520;            
            strengthBar = new SliderBar(game, 500, strengthArray.Length, barPosition);            
            
            barPosition.Y = 646;
            flexBar = new SliderBar(game, 500, flexArray.Length, barPosition);                                    
                        
            Components.Add(weightBar);
            Components.Add(strengthBar);
            Components.Add(flexBar);                                
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public SliderBar WeightAttribute
        {
            get { return weightBar; }
            set { weightBar = value; }
        }

        public SliderBar StrengthAttribute
        {
            get { return strengthBar; }
            set { strengthBar = value; }
        }

        public SliderBar FlexibilityAttribute
        {
            get { return flexBar; }
            set { flexBar = value; }
        }

        public override void Show()
        {
            base.Show();
        }

        public override void Hide()
        {
            base.Hide();
        }

        /// <summary>
        /// Calls the getBarAttribute method of each slider bar to return the partition the slide button is in.
        /// The method then sets the strings the SpriteBatch will use the draw the information in the game window.
        /// </summary>
        public void SetAttributeValues()
        {                      
            arrayIndex = weightBar.getBarAttribute;
            wValue = (weightArray[arrayIndex]).ToString();
            weight = weightArray[arrayIndex];
            fileStatManager.weightStat = weightArray[arrayIndex];

            arrayIndex = strengthBar.getBarAttribute;            
            strValue = (strengthArray[arrayIndex]).ToString();
            strength = strengthArray[arrayIndex];
            fileStatManager.strengthStat = strengthArray[arrayIndex];

            arrayIndex = flexBar.getBarAttribute;
            flxValue = (flexArray[arrayIndex]).ToString();
            flex = flexArray[arrayIndex];
            fileStatManager.flexibilityStat = flexArray[arrayIndex];
        }

        private void handleSounds()
        {
            if((oldWeight!=weight)||(oldFlex!=flex)||(oldStrength!=strength))
                if(!first)
                    audioManager.PlayCue("btnclick5");
        }

      
        public override void Update(GameTime gameTime)
        {
            SetAttributeValues();                        

            base.Update(gameTime);            
        }

        /// <summary>
        /// Draws the background, the slider backgrounds, and the attribute strings.
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(background, new Rectangle(0, 0, 1280, 1024), Color.White);            
                        
            spriteBatch.DrawString(normalFont, wValue,   new Vector2(1080, 389), Color.White);
            spriteBatch.DrawString(normalFont, strValue, new Vector2(1080, 515), Color.White);
            spriteBatch.DrawString(normalFont, flxValue, new Vector2(1080, 641), Color.White);

            handleSounds();

            oldFlex = flex;
            oldStrength = strength;
            oldWeight = weight;
            if (first)
                first = !first;
            base.Draw(gameTime);
        }
    }
}